package net.neptech.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.neptech.bo.AddStaffBo;
import net.neptech.bo.StaffBo;
import net.neptech.bo.StaffInfoBo;
import net.neptech.dto.sys.StaffDTO;
import net.neptech.mapper.*;
import net.neptech.model.sys.Staff;
import net.neptech.model.sys.StaffPosition;
import net.neptech.model.sys.StaffRole;
import net.neptech.page.PageQuery;
import net.neptech.page.TableDataInfo;
import net.neptech.repository.StaffRepository;
import net.neptech.service.StaffService;
import net.neptech.utils.BeanCopyUtils;
import net.neptech.vo.DeptStaffVo;
import net.neptech.vo.PositionVo;
import net.neptech.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Saya
 * @Date 2025/8/13 17:34
 * @Description TODO
 **/
@Slf4j
@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffRepository staffRepository;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private StaffRoleMapper staffRoleMapper;
    @Autowired
    private StaffPositionMapper staffPositionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionMapper positionMapper;

    @Override
    public List<StaffDTO> getExpert() {
        //查询副主任医师以上级别
        return staffMapper.getExpert();
    }

    @Override
    public List<StaffDTO> getStaff() {
        return staffMapper.getStaffs();
    }

    @Override
    public List<StaffDTO> getStaffByDeptId(Long deptId) {
        return staffMapper.getStaffsByDeptId(deptId);
    }

    @Override
    public List<Staff> getByDeptId(Long deptId) {
        System.out.println(staffMapper.getByDeptId(deptId));
        return staffMapper.getByDeptId(deptId);
    }

    @Override
    public List<DeptStaffVo> getStaffByStaffId(List<Long> staffId) {

        return staffMapper.getStaffsByIds(staffId);
    }

    @Override
    public TableDataInfo<StaffVo> selectPageUserList(StaffBo staffBo, PageQuery pageQuery) {
        Page<StaffVo> page = staffMapper.selectPageUserList(pageQuery.build(), staffBo);
        page.getRecords().forEach(staff -> {
            List<PositionVo> positionVos = staffPositionMapper.selectByStaffId(staff.getStaffId());
            staff.setPositions(positionVos);
        });
        return TableDataInfo.build(page);
    }

    @Override
    public StaffVo selectStaffById(Long staffId) {
        return BeanCopyUtils.copyBean(staffMapper.selectById(staffId), StaffVo.class);
    }

    @Override
    @Transactional
    public void updateStaffInfo(StaffInfoBo staffInfoBo) {
        //更新员工信息
        staffMapper.updateById(BeanCopyUtils.copyBean(staffInfoBo, Staff.class));
        //设置员工角色，删除插入
        staffRoleMapper.delete(new LambdaQueryWrapper<>(StaffRole.class).eq(StaffRole::getStaffId, staffInfoBo.getStaffId()));
        Arrays.stream(staffInfoBo.getRoleIds()).forEach(staffRoleId -> {
            StaffRole staffRole = new StaffRole();
            staffRole.setStaffId(staffInfoBo.getStaffId());
            staffRole.setRoleId(staffRoleId);
            staffRoleMapper.insert(staffRole);
        });
        //清空员工职位信息并插入
        staffPositionMapper.delete(new LambdaQueryWrapper<>(StaffPosition.class).eq(StaffPosition::getStaffId, staffInfoBo.getStaffId()));
        Arrays.stream(staffInfoBo.getPositionIds()).forEach(staffPositionId -> {
            StaffPosition staffPosition = new StaffPosition();
            staffPosition.setStaffId(staffInfoBo.getStaffId());
            staffPosition.setPositionId(staffPositionId);
            staffPositionMapper.insert(staffPosition);
        });
    }

    @Override
    @Transactional
    public void insertStaff(AddStaffBo addStaffBo) {
        //新增员工
        Staff staff = BeanCopyUtils.copyBean(addStaffBo, Staff.class);
        staffMapper.insert(staff);
        //插入职称表数据
        Arrays.stream(addStaffBo.getPositionIds()).forEach(positionId -> {
            StaffPosition staffPosition = new StaffPosition();
            staffPosition.setPositionId(positionId);
            staffPosition.setStaffId(staff.getStaffId());
            staffPositionMapper.insert(staffPosition);
        });
        if (addStaffBo.getRoleIds() != null) {
            //插入角色表数据
            Arrays.stream(addStaffBo.getRoleIds()).forEach(roleId -> {
                StaffRole staffRole = new StaffRole();
                staffRole.setRoleId(roleId);
                staffRole.setStaffId(staff.getStaffId());
                staffRoleMapper.insert(staffRole);
            });
        }
    }

    /**
     * @Author Saya
     * @Date 2025/8/20 14:55
     * @Description 用户id查员工DTO
     * @Params [userId]
     * @Return java.util.List<net.neptech.dto.sys.StaffDTO>
     **/
    @Override
    public StaffDTO getOneByUserId(Long userId) {
        return staffMapper.getStaffByUserId(userId);
    }

    @Override
    public List<Long> getIdsByDeptIds(List<Long> deptIds) {
        List<Long> ids = new ArrayList<>();
        staffRepository.findByDeptIdIn(deptIds).forEach(staff -> {ids.add(staff.getStaffId());});
        return ids;
    }

    @Override
    public List<Long> getIdsByRoleIds(List<Long>  roleIds) {
        return staffMapper.getIdsByRoleIds(roleIds);
    }

    @Override
    public List<StaffDTO> searchByName(String userName) {
        return staffMapper.searchByName(userName);
    }


    @Override
    public Staff getStaffInfoByUserId(Long userId) {
        return staffMapper.selectOne(new LambdaQueryWrapper<Staff>().eq(Staff::getUserId, userId));
    }
}
